﻿using System;
using System.IO;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections.Generic;
using System.Drawing.Printing;

namespace MyNotepad
{
    public partial class MainWindow : Form
    {
        public string[] Args;
        TabbedNote activeNote = null;

        public MainWindow()
        {
            InitializeComponent();
        }

        public MainWindow(string file)
        {
            InitializeComponent();
        }

        public delegate void ProcessParametersDelegate(object sender, string[] args);
        public void ProcessParameters(object sender, string[] args)
        {
            // The form has loaded, and initialization will have been be done.

            // Add the command-line arguments to our textbox, just to confirm that
            // it reached here.
            if (args.Length == 0)
            {
                TabbedNote note = new TabbedNote();
                note.Show(mainDockPanel);
            }
            else
            {
                foreach (string file in args)
                {
                    FileInfo f = new FileInfo(file);
                    TabbedNote note = new TabbedNote(f);
                    note.Show(this.mainDockPanel);
                    this.Focus();
                }
            }
        }

        private void NewTabbedNote(object sender, EventArgs e)
        {
            TabbedNote note = new TabbedNote();
            note.Show(this.mainDockPanel);
        }

        private void OpenTabbedNote(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "Open...";
            string filter = "";

            filter += "All Files (*.*)|*.*|";
            filter += "C Files (*.c)|*.c|";
            filter += "C++ Files (*.cpp)|*.cpp|";
            filter += "C# Files (*.cs)|*.cs|";
            filter += "Java Files (*.java)|*.java|";
            filter += "HTML Files (*.html)|*.html|";
            filter += "Text Files (*.txt)|*.txt";
            
            open.Filter = filter;
            open.DefaultExt = ".txt";
            open.Multiselect = true;
            if (open.ShowDialog() == DialogResult.OK)
            {
                FileInfo f;
                foreach (String s in open.FileNames)
                {
                    f = new FileInfo(s);
                    TabbedNote note = new TabbedNote(f);
                    note.Show(this.mainDockPanel);
                }
            }
        }

        private void SaveCurrentNote(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.Save();
        }

        private void SaveFileAs(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.SaveFileAs();
        }

        private void MyNotepadExit(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void Exiting()
        {
            DockContentCollection openContents = mainDockPanel.Contents;
            foreach (DockContent c in openContents)
            {
                TabbedNote note = (TabbedNote)c;
                note.Close();
            }
        }

        private void MyNotepadMainFormClosed(object sender, FormClosingEventArgs e)
        {
            //Exiting();            
        }

        private void MainWindowOpened(object sender, EventArgs e)
        {
            if (this.Args != null)
            {
                ProcessParameters(null, this.Args);
                this.Args = null;
            }
        }

        private void UndoLastAction(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.UndoRedo.Undo();
        }

        private void RedoLastUndone(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.UndoRedo.Redo();
        }

        private void ContentChanged(object sender, EventArgs e)
        {
            try
            {
                activeNote = (TabbedNote)mainDockPanel.ActiveContent;                
                if (activeNote.Info == null)
                {
                    lblActiveFileInfo.Text = "Document Path: Unsaved";
                    this.Text = "My Notepad";
                }
                else
                {
                    lblActiveFileInfo.Text = "Document Path: " + activeNote.Info.FullName;
                    this.Text = activeNote.Info.Name + " - My Notepad";
                }
            }
            catch (Exception) { }
        }

        private void CloseTab(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.Close();
        }

        private void CutText(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.Clipboard.Cut();
        }

        private void CopyText(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.Clipboard.Copy();
        }

        private void PasteText(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.Clipboard.Paste();
        }

        private void SelectAllText(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.Selection.SelectAll();
        }

        private void ShowAboutBox(object sender, EventArgs e)
        {
            AboutWindow abt = new AboutWindow();
            abt.ShowDialog();
        }

        private void PrintCurrentDocument(object sender, EventArgs e)
        {
            try
            {
                if (activeNote != null)
                    activeNote.ScintillaContent.Printing.Print(true);
            }
            catch (InvalidPrinterException)
            {
                MessageBox.Show("You have no printers installed. Please install one.", "My Notepad", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void PreviewCurrentDocumentPrinting(object sender, EventArgs e)
        {
            try
            {
                if (activeNote != null)
                    activeNote.ScintillaContent.Printing.PrintPreview();
            }
            catch (InvalidPrinterException)
            {                
                MessageBox.Show("You have no printers installed. Please install one.", "My Notepad", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Replace(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.FindReplace.ShowReplace();
        }

        private void Find(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.FindReplace.ShowFind();
        }

        private void ImmediatePrint(object sender, EventArgs e)
        {
            try
            {
                if (activeNote != null)
                    activeNote.ScintillaContent.Printing.Print(false);
            }
            catch (InvalidPrinterException)
            {
                MessageBox.Show("You have no printers installed. Please install one.", "My Notepad", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ZoomIn(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.Zoom++;
        }

        private void ZoomOut(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.Zoom--;
        }

        private void ResetZoom(object sender, EventArgs e)
        {
            if (activeNote != null)
                activeNote.ScintillaContent.Zoom = 0;
        }

        private void ToggleLineNumbers(object sender, EventArgs e)
        {
            if (activeNote != null)
            {
                if (activeNote.ScintillaContent.Margins[0].Width == 50)
                {
                    activeNote.ScintillaContent.Margins[0].Width = 0;
                    lineNumbersToolStripMenuItem.Checked = false;
                }
                else
                {
                    activeNote.ScintillaContent.Margins[0].Width = 50;
                    lineNumbersToolStripMenuItem.Checked = true;
                }
            }
        }

        private void CloseOthers(object sender, EventArgs e)
        {
            foreach (Form frm in this.MdiChildren)
            {
                if (frm != activeNote)
                    frm.Close();
            }
        }

        private void SaveAll(object sender, EventArgs e)
        {
            DockContentCollection open = mainDockPanel.Contents;
            foreach (TabbedNote note in open)
            {
                note.Save();
            }
        }

        private void CloseAll(object sender, EventArgs e)
        {
            foreach (Form frm in this.MdiChildren)
                frm.Close();
        }
    }
}
